const Throttle = require("../template")

function testThrottleComplexScenarios() {
  console.log(
    "1. 节流函数复杂场景 - 嵌套调用测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttle(() => {
        callCount++
        // 在节流函数内部再次调用
        throttled()
      }, 100)
      
      throttled()
      
      return callCount === 1 // 嵌套调用应该只执行一次
    })()
  )

  console.log(
    "2. 节流函数复杂场景 - 链式调用测试:",
    (() => {
      let result = ''
      const throttled = Throttle.throttle(() => {
        result += 'A'
      }, 100)
      
      throttled()
      throttled()
      throttled()
      
      return result === 'A' // 链式调用应该只执行一次
    })()
  )

  console.log(
    "3. 节流函数复杂场景 - 异步调用测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttle(() => {
        callCount++
      }, 100)
      
      setTimeout(() => throttled(), 10)
      setTimeout(() => throttled(), 20)
      setTimeout(() => throttled(), 30)
      
      return callCount === 1 // 异步调用应该只执行一次
    })()
  )

  console.log(
    "4. 节流函数复杂场景 - 事件模拟测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttle(() => {
        callCount++
      }, 100)
      
      // 模拟事件触发
      for (let i = 0; i < 10; i++) {
        setTimeout(() => throttled(), i * 10)
      }
      
      return callCount === 1 // 事件模拟应该只执行一次
    })()
  )

  console.log(
    "5. 节流函数复杂场景 - 条件分支测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttleWithCondition(() => {
        callCount++
      }, 100, (args) => {
        return args[0] % 2 === 0
      })
      
      throttled(1) // 奇数，不满足条件
      throttled(2) // 偶数，满足条件
      throttled(3) // 奇数，不满足条件
      
      return callCount === 1 // 条件分支应该只执行一次
    })()
  )

  console.log(
    "6. 节流函数复杂场景 - 优先级冲突测试:",
    (() => {
      let callCount = 0
      const highPriority = Throttle.throttleWithPriority(() => {
        callCount++
      }, 100, 10)
      
      const lowPriority = Throttle.throttleWithPriority(() => {
        callCount++
      }, 100, 1)
      
      lowPriority()
      highPriority()
      lowPriority()
      
      return callCount === 2 // 优先级冲突应该都执行
    })()
  )

  console.log(
    "7. 节流函数复杂场景 - 批量处理测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttleWithBatch(() => {
        callCount++
      }, 100, 5)
      
      throttled()
      throttled()
      throttled()
      throttled()
      throttled()
      
      return callCount === 1 // 批量处理应该只执行一次
    })()
  )

  console.log(
    "8. 节流函数复杂场景 - 错误恢复测试:",
    (() => {
      let callCount = 0
      let errorCount = 0
      const throttled = Throttle.throttleWithErrorHandling(() => {
        callCount++
        throw new Error('test error')
      }, 100, (error) => {
        errorCount++
      })
      
      throttled()
      throttled()
      
      return callCount === 1 && errorCount === 1 // 错误恢复应该执行一次
    })()
  )

  console.log(
    "9. 节流函数复杂场景 - 进度跟踪测试:",
    (() => {
      let callCount = 0
      let progressCount = 0
      const throttled = Throttle.throttleWithProgress(() => {
        callCount++
      }, 100, (progress) => {
        progressCount++
      })
      
      throttled()
      throttled()
      throttled()
      
      return callCount === 1 && progressCount === 0 // 进度跟踪不应该执行
    })()
  )

  console.log(
    "10. 节流函数复杂场景 - 统计监控测试:",
    (() => {
      let callCount = 0
      const { throttled, getStats } = Throttle.throttleWithStats(() => {
        callCount++
      }, 100)
      
      throttled()
      throttled()
      throttled()
      
      const stats = getStats()
      return callCount === 1 && typeof stats === 'object' // 统计监控应该工作
    })()
  )

  console.log(
    "11. 节流函数复杂场景 - 取消后重新创建测试:",
    (() => {
      let callCount = 0
      const { throttled, cancel } = Throttle.throttleWithCancel(() => {
        callCount++
      }, 100)
      
      throttled()
      cancel()
      
      // 重新创建节流函数
      const newThrottled = Throttle.throttle(() => {
        callCount++
      }, 100)
      
      newThrottled()
      
      return callCount === 1 // 重新创建后应该执行一次
    })()
  )

  console.log(
    "12. 节流函数复杂场景 - 刷新后状态重置测试:",
    (() => {
      let callCount = 0
      const { throttled, flush, isPending } = Throttle.throttleWithFlush(() => {
        callCount++
      }, 100)
      
      throttled()
      flush()
      throttled()
      
      return callCount === 2 && isPending() === false // 刷新后状态应该重置
    })()
  )

  console.log(
    "13. 节流函数复杂场景 - 状态查询一致性测试:",
    (() => {
      const { throttled, isPending, isCancelled } = Throttle.throttleWithState(() => {}, 100)
      
      throttled()
      const pending1 = isPending()
      const cancelled1 = isCancelled()
      
      throttled()
      const pending2 = isPending()
      const cancelled2 = isCancelled()
      
      return pending1 === false && cancelled1 === false && 
             pending2 === false && cancelled2 === false // 状态应该一致
    })()
  )

  console.log(
    "14. 节流函数复杂场景 - 多实例隔离测试:",
    (() => {
      let callCount1 = 0
      let callCount2 = 0
      
      const throttled1 = Throttle.throttle(() => {
        callCount1++
      }, 100)
      
      const throttled2 = Throttle.throttle(() => {
        callCount2++
      }, 100)
      
      throttled1()
      throttled2()
      
      return callCount1 === 1 && callCount2 === 1 // 多实例应该隔离
    })()
  )

  console.log(
    "15. 节流函数复杂场景 - 边界条件测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttle(() => {
        callCount++
      }, 0) // 延迟时间为0
      
      throttled()
      
      return callCount === 1 // 边界条件应该执行一次
    })()
  )

  console.log(
    "16. 节流函数复杂场景 - 极端参数测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttle(() => {
        callCount++
      }, 100)
      
      // 传递极端参数
      throttled(null, undefined, NaN, Infinity, -Infinity)
      
      return callCount === 1 // 极端参数应该执行一次
    })()
  )

  console.log(
    "17. 节流函数复杂场景 - 内存压力测试:",
    (() => {
      const throttledFunctions = []
      
      // 创建大量节流函数
      for (let i = 0; i < 1000; i++) {
        throttledFunctions.push(Throttle.throttle(() => {}, 100))
      }
      
      // 调用所有函数
      for (let i = 0; i < 1000; i++) {
        throttledFunctions[i]()
      }
      
      return throttledFunctions.length === 1000 // 内存压力测试应该通过
    })()
  )

  console.log(
    "18. 节流函数复杂场景 - 并发安全测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttle(() => {
        callCount++
      }, 100)
      
      // 模拟并发调用
      const promises = []
      for (let i = 0; i < 100; i++) {
        promises.push(Promise.resolve().then(() => throttled()))
      }
      
      return Promise.all(promises).then(() => {
        return callCount === 1 // 并发安全应该保证
      })
    })()
  )
}

testThrottleComplexScenarios()
